package com.bdmc.web.controller.task;

import java.util.List;
import java.util.Arrays;
import java.util.Map;

import com.bdmc.task.domain.bo.TaskPlanDetailBo;
import com.bdmc.task.domain.bo.TaskTeamSpanBo;
import com.bdmc.task.domain.vo.TaskPlanDetailVo;
import com.bdmc.task.domain.vo.TaskTeamSpanVo;
import com.bdmc.task.service.ITaskPlanDetailService;
import com.bdmc.task.service.ITaskTeamSpanService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.bdmc.common.annotation.RepeatSubmit;
import com.bdmc.common.annotation.Log;
import com.bdmc.common.core.controller.BaseController;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.domain.AjaxResult;
import com.bdmc.common.core.validate.AddGroup;
import com.bdmc.common.core.validate.EditGroup;
import com.bdmc.common.core.validate.QueryGroup;
import com.bdmc.common.enums.BusinessType;
import com.bdmc.common.utils.poi.ExcelUtil;
import com.bdmc.task.domain.vo.TaskWorkPlanVo;
import com.bdmc.task.domain.bo.TaskWorkPlanBo;
import com.bdmc.task.service.ITaskWorkPlanService;
import com.bdmc.common.core.page.TableDataInfo;


/**
 * 排班计划控制器
 *
 * @author bdmc
 * @date 2022-03-06
 */
@Validated
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/task/plan")
public class TaskWorkPlanController extends BaseController {

    private final ITaskWorkPlanService iTaskWorkPlanService;
    private final ITaskPlanDetailService taskPlanDetailService;
    private final ITaskTeamSpanService iTaskTeamSpanService;

    /**
     * 查询排班计划列表
     */
    @SaCheckPermission("task:plan:list")
    @GetMapping("/list")
    public TableDataInfo<TaskWorkPlanVo> list(@Validated(QueryGroup.class) TaskWorkPlanBo bo, PageQuery pageQuery) {
        TableDataInfo<TaskWorkPlanVo> tableDataInfo = iTaskWorkPlanService.mySelectPage(bo, pageQuery);
        for (TaskWorkPlanVo row : tableDataInfo.getRows()) {
            List<TaskPlanDetailVo> details = taskPlanDetailService.selectTaskPlanDetailByWorkPlanId(row.getWorkPlanId());
            row.setPlanDetailList(details);
        }
        return tableDataInfo;
    }

    /**
     * 导出排班计划列表
     */
    @SaCheckPermission("task:plan:export")
    @Log(title = "排班计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated TaskWorkPlanBo bo, HttpServletResponse response) {
        List<TaskWorkPlanVo> list = iTaskWorkPlanService.queryList(bo);
        ExcelUtil.exportExcel(list, "排班计划", TaskWorkPlanVo.class, response);
    }

    /**
     * 获取排班计划详细信息
     */
    @SaCheckPermission("task:plan:query")
    @GetMapping("/{workPlanId}")
    public AjaxResult<TaskWorkPlanVo> getInfo(@PathVariable("workPlanId") Integer workPlanId) {
        return AjaxResult.success(iTaskWorkPlanService.queryById(workPlanId));
    }

    /**
     * 新增排班计划
     */
    @SaCheckPermission("task:plan:add")
    @Log(title = "排班计划", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody TaskWorkPlanBo bo) {
        if(bo.getDeptId()==null||bo.getDeptId()==0){
            bo.setDeptId(getDeptId());
        }
        return toAjax(iTaskWorkPlanService.insertByBo(bo));
    }

    /**
     * 修改排班计划
     */
    @SaCheckPermission("task:plan:edit")
    @Log(title = "排班计划", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody TaskWorkPlanBo bo) {
        return toAjax(iTaskWorkPlanService.updateByBo(bo));
    }

    /**
     * 删除排班计划
     */
    @SaCheckPermission("task:plan:remove")
    @Log(title = "排班计划" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{workPlanIds}")
    public AjaxResult<Void> remove(@PathVariable Integer[] workPlanIds) {
        return toAjax(iTaskWorkPlanService.deleteWithValidByIds(Arrays.asList(workPlanIds), true));
    }

    /**
     * 通过deptId 查询当前日期的排班车辆
     * @param bo 包含deptId
     * @return taskTeamList
     */
    @GetMapping("car")
    public AjaxResult<List<Map<String,Object>>> workingCar(TaskTeamSpanBo bo){
        return AjaxResult.success(iTaskTeamSpanService.selectWorkingCar(bo));
    }

    @GetMapping("person")
    public AjaxResult<List<Map<String,Object>>> workingPerson(TaskTeamSpanBo bo){
        return AjaxResult.success(iTaskTeamSpanService.selectWorkingPerson(bo));
    }
}
